package com.ruoyi.web.controller.consumables;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.consumables.domain.Consumables;
import com.ruoyi.consumables.service.IConsumablesService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 耗材记录Controller
 *
 * @author ruoyi
 * @date 2023-04-22
 */
@Controller
@RequestMapping("/consumables/consumables")
public class ConsumablesController extends BaseController
{
    private String prefix = "consumables/consumables";

    @Autowired
    private IConsumablesService consumablesService;

    @RequiresPermissions("consumables:consumables:view")
    @GetMapping()
    public String consumables()
    {
        return prefix + "/consumables";
    }

    /**
     * 查询耗材记录列表
     */
    @RequiresPermissions("consumables:consumables:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Consumables consumables)
    {
        startPage();
        List<Consumables> list = consumablesService.selectConsumablesList(consumables);
        return getDataTable(list);
    }

    /**
     * 导出耗材记录列表
     */
    @RequiresPermissions("consumables:consumables:export")
    @Log(title = "耗材记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Consumables consumables)
    {
        List<Consumables> list = consumablesService.selectConsumablesList(consumables);
        ExcelUtil<Consumables> util = new ExcelUtil<Consumables>(Consumables.class);
        return util.exportExcel(list, "耗材记录数据");
    }

    /**
     * 新增耗材记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存耗材记录
     */
    @RequiresPermissions("consumables:consumables:add")
    @Log(title = "耗材记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Consumables consumables)
    {
        return toAjax(consumablesService.insertConsumables(consumables));
    }

    /**
     * 修改耗材记录
     */
    @RequiresPermissions("consumables:consumables:edit")
    @GetMapping("/edit/{consumablesId}")
    public String edit(@PathVariable("consumablesId") Long consumablesId, ModelMap mmap)
    {
        Consumables consumables = consumablesService.selectConsumablesByConsumablesId(consumablesId);
        mmap.put("consumables", consumables);
        return prefix + "/edit";
    }

    /**
     * 修改保存耗材记录
     */
    @RequiresPermissions("consumables:consumables:edit")
    @Log(title = "耗材记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Consumables consumables)
    {
        return toAjax(consumablesService.updateConsumables(consumables));
    }

    /**
     * 删除耗材记录
     */
    @RequiresPermissions("consumables:consumables:remove")
    @Log(title = "耗材记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(consumablesService.deleteConsumablesByConsumablesIds(ids));
    }
}
